import React from 'react';
import {Spin, Card, Button, Table} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {currencyHis, shipmentTypeHis, orderTypeHis, partOrderPriceTermsHis, paymentHisType} from '../Enum';
import routes from './routes';
import {DATE_FORMAT, TABLE} from '../constants';
import {conventEnumValueToString, formatDateTime, formatAmount, onDownloadHistory} from '../utils';
import {getHistoryDetail} from './api';
import {injectIntl} from './intl';
import styles from './style.css';
const {Description} = DescriptionList;

class HistoryDetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: [],
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getHistoryDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details.map(d => Object.assign({}, d, {
                            order: res.data.details.findIndex(x => x.id === d.id) + 1,
                        })),
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }

    onCancel = () => {
        this.props.history.push(routes.historyQuery.url());
    }
    onClickDownload = e => {
        const {id, applyid} = e.target.dataset;
        onDownloadHistory({
            filePath: id,
            fileName: applyid
        });
    }
    render() {
        const {data, details} = this.state;
        const {attachments = []} = data;
        const {formatMessage} = this.props.intl;
        let totalQuantity = 0;
        let totalAmount = 0;
        if(details)
            details.forEach(d => {
                totalQuantity += (d.quantity);
                totalAmount += (d.amount);
            });
        const detailColums = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.order',
                defaultMessage: '序号'
            }),
            dataIndex: 'order',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partCode',
                defaultMessage: '备件编码'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.newPartCode',
                defaultMessage: '被替换件编号'
            }),
            dataIndex: 'newPartCode'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.quantity',
                defaultMessage: '订购数量'
            }),
            dataIndex: 'quantity'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.amount',
                defaultMessage: '金额'
            }),
            dataIndex: 'amount',
            render: text => formatAmount(text)
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.partPiHistory',
                            defaultMessage: '备件PI历史详情'
                        })}>
                            {!this.props.isDealerUser &&
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.dealerCode',
                                defaultMessage: '总代编号'
                            })}>{data.dealerCode}
                            </Description>
                            }
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.submitTime',
                                defaultMessage: '提报日期'
                            })}>{formatDateTime(data.submitTime, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.country',
                                defaultMessage: '国家'
                            })}>{data.country}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.partOrderCode',
                                defaultMessage: '备件订单号'
                            })}>{data.partOrderCode}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.orderTypeHis',
                                defaultMessage: '订单类型'
                            })}>{conventEnumValueToString(orderTypeHis, data.orderTypeHis)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.paymentHisType',
                                defaultMessage: '付款方式'
                            })}>{conventEnumValueToString(paymentHisType, data.paymentHisType)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.customerOrderCode',
                                defaultMessage: '客户订单号'
                            })}>{data.customerOrderCode}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.receivedPerson',
                                defaultMessage: '收货联系人'
                            })}>{data.receivedPerson}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.partOrderPriceTermsHis',
                                defaultMessage: '价格条款'
                            })}>{conventEnumValueToString(partOrderPriceTermsHis, data.partOrderPriceTermsHis)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.receivedPersonPhone',
                                defaultMessage: '联系电话'
                            })}>{data.receivedPersonPhone}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.carier',
                                defaultMessage: '指定货代'
                            })}>{data.carier}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.shipmentTypeHis',
                                defaultMessage: '发运方式'
                            })}>{conventEnumValueToString(shipmentTypeHis, data.shipmentTypeHis)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.receivedPort',
                                defaultMessage: '目的地港口'
                            })}>{data.receivedPort}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.currencyHis',
                                defaultMessage: '币种'
                            })}>{conventEnumValueToString(currencyHis, data.currencyHis)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.shipmentPort',
                                defaultMessage: '发运港口'
                            })}>{data.shipmentPort}</Description>
                        </DescriptionList>
                        <DescriptionList col="1" className={styles.detailDescriptionList}>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.receivedAddress',
                                defaultMessage: '收货地址'
                            })}>{data.receivedAddress}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.details',
                            defaultMessage: '发票清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={detailColums}
                                rowKey="id"
                                pagination={false}
                                dataSource={details}
                                footer={() => <DescriptionList>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalQuantity',
                                        defaultMessage: '总数量'
                                    })}>{totalQuantity}
                                    </Description>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalAmount',
                                        defaultMessage: '总金额'
                                    })}>{formatAmount(totalAmount)}
                                    </Description>
                                </DescriptionList>}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    {attachments && attachments.length >= 0
                        ? <Card><DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.attachment',
                            defaultMessage: '历史附件'
                        })}>
                            {attachments.map(item => (<Description key={item.id}>
                                <a download onClick={this.onClickDownload} key="delete" data-id={item.filePath} data-applyid={item.fileName}>
                                    {item.fileName}</a></Description>))}
                        </DescriptionList></Card>
                        : <Card><DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList></Card>}
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{formatMessage({
                        id: 'historyDetailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HistoryDetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getDealerUser = selectorFactory(['page', 'domain', 'initData', 'isDealerUser']);

const mapStateToProps = state => ({
    isDealerUser: getDealerUser(state),
});

export default connect(mapStateToProps, null)(injectIntl(HistoryDetailPanel));
